using System;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Runtime.Serialization;
using OutSystems.ObjectKeys;
using OutSystems.RuntimeCommon;
using OutSystems.HubEdition.RuntimePlatform;
using GotDotNet.ApplicationBlocks;
using OutSystems.HubEdition.RuntimePlatform.Db;

namespace OutSystems.NssRemotingRadarsAdapter {

	/// <summary>
	/// Structure <code>STEvento_RRStructure</code> that represents the Service Studio structure
	///  <code>Evento_RR</code> <p> Description: Representação da estrutura de 'Evento' recolhida de radare
	/// s do tipo 'RemotingRadars'</p>
	/// </summary>
	[Serializable()]
	public struct STEvento_RRStructure: ISerializable, ISimpleRecord {
		private static readonly GlobalObjectKey OriginalKeyId = GlobalObjectKey.Parse("rRLhx6COuEqSumYLT_VGZg*P0P1hedkdEidhNY823HrJw");
		private static readonly GlobalObjectKey OriginalKeyVelocidade = GlobalObjectKey.Parse("rRLhx6COuEqSumYLT_VGZg*Nd_4C2qhsUumHOhAKyixQA");
		private static readonly GlobalObjectKey OriginalKeyMatricula = GlobalObjectKey.Parse("rRLhx6COuEqSumYLT_VGZg*igiLU3zFAkKYgk1yRr7klA");
		private static readonly GlobalObjectKey OriginalKeyImagem = GlobalObjectKey.Parse("rRLhx6COuEqSumYLT_VGZg*LtPhtUGlB0aLnzSSft7Byg");
		private static readonly GlobalObjectKey OriginalKeyNomeLocal = GlobalObjectKey.Parse("rRLhx6COuEqSumYLT_VGZg*MmDbgpK_CEG_ctuezsq_AA");
		private static readonly GlobalObjectKey OriginalKeyUIDLocal = GlobalObjectKey.Parse("rRLhx6COuEqSumYLT_VGZg*k9PQTekan0yVwAs+qWRIlg");
		private static readonly GlobalObjectKey OriginalKeyLimiteVelocidade = GlobalObjectKey.Parse("rRLhx6COuEqSumYLT_VGZg*udgw9Lrg0EOBwPUXNYdGtg");
		private static readonly GlobalObjectKey OriginalKeyActivo = GlobalObjectKey.Parse("rRLhx6COuEqSumYLT_VGZg*_aHiKzbUuECDDjc6_wgQkg");

		public static readonly GlobalObjectKey OriginalKey = GlobalObjectKey.Parse("rRLhx6COuEqSumYLT_VGZg*SKNoJpCBTEmjz+9K+vKOrQ");
		[System.Xml.Serialization.XmlElement("Id")]
		public int ssId;

		[System.Xml.Serialization.XmlElement("Velocidade")]
		public decimal ssVelocidade;

		[System.Xml.Serialization.XmlElement("Matricula")]
		public string ssMatricula;

		[System.Xml.Serialization.XmlElement("Imagem")]
		public byte[] ssImagem;

		[System.Xml.Serialization.XmlElement("NomeLocal")]
		public string ssNomeLocal;

		[System.Xml.Serialization.XmlElement("UIDLocal")]
		public string ssUIDLocal;

		[System.Xml.Serialization.XmlElement("LimiteVelocidade")]
		public decimal ssLimiteVelocidade;

		[System.Xml.Serialization.XmlElement("Activo")]
		public bool ssActivo;


		public BitArray OptimizedAttributes;

		public STEvento_RRStructure(params string[] dummy) {
			OptimizedAttributes = null;
			ssId = 0;
			ssVelocidade = 0.0M;
			ssMatricula = string.Empty;
			ssImagem = new byte[] {};
			ssNomeLocal = string.Empty;
			ssUIDLocal = string.Empty;
			ssLimiteVelocidade = 0.0M;
			ssActivo = false;
		}

		/// <summary>
		/// Read a record from database
		/// </summary>
		/// <param name="r"> Data base reader</param>
		/// <param name="index"> index</param>
		public void Read(IDataReader r, ref int index) {
			Object column = null;
			String columnName = "";
			try {
				columnName = "Evento_RR.Id";
				column = r[index + 0];
				if(column == System.DBNull.Value || column is int) {
					ssId = (column != System.DBNull.Value ?(int) column: 0);
				} else {
					ssId = Convert.ToInt32(column);
				}
				columnName = "Evento_RR.Velocidade";
				column = r[index + 1];
				if(column == System.DBNull.Value || column is decimal) {
					ssVelocidade = (column != System.DBNull.Value ?(decimal) column: 0.0M);
				} else {
					ssVelocidade = Convert.ToDecimal(column, FormatInfo.Number);
				}
				columnName = "Evento_RR.Matricula";
				column = r[index + 2];
				if(column == System.DBNull.Value || column is string) {
					ssMatricula = (column != System.DBNull.Value ?(string) column: string.Empty);
				} else {
					ssMatricula = (column is DateTime?((DateTime) column).ToString(FormatInfo.GetOutputDateTimeFormatString(), FormatInfo.defaultHeCultureInfo): Convert.ToString(column));
				}
				if(DBTransactionManager.Current.DBKind == DBKind.Oracle && ssMatricula == " ") ssMatricula = string.Empty;
				columnName = "Evento_RR.Imagem";
				column = r[index + 3];
				ssImagem = (column != System.DBNull.Value ?(byte[]) column: new byte[] {}
				);
				columnName = "Evento_RR.NomeLocal";
				column = r[index + 4];
				if(column == System.DBNull.Value || column is string) {
					ssNomeLocal = (column != System.DBNull.Value ?(string) column: string.Empty);
				} else {
					ssNomeLocal = (column is DateTime?((DateTime) column).ToString(FormatInfo.GetOutputDateTimeFormatString(), FormatInfo.defaultHeCultureInfo): Convert.ToString(column));
				}
				if(DBTransactionManager.Current.DBKind == DBKind.Oracle && ssNomeLocal == " ") ssNomeLocal = string.Empty;
				columnName = "Evento_RR.UIDLocal";
				column = r[index + 5];
				if(column == System.DBNull.Value || column is string) {
					ssUIDLocal = (column != System.DBNull.Value ?(string) column: string.Empty);
				} else {
					ssUIDLocal = (column is DateTime?((DateTime) column).ToString(FormatInfo.GetOutputDateTimeFormatString(), FormatInfo.defaultHeCultureInfo): Convert.ToString(column));
				}
				if(DBTransactionManager.Current.DBKind == DBKind.Oracle && ssUIDLocal == " ") ssUIDLocal = string.Empty;
				columnName = "Evento_RR.LimiteVelocidade";
				column = r[index + 6];
				if(column == System.DBNull.Value || column is decimal) {
					ssLimiteVelocidade = (column != System.DBNull.Value ?(decimal) column: 0.0M);
				} else {
					ssLimiteVelocidade = Convert.ToDecimal(column, FormatInfo.Number);
				}
				columnName = "Evento_RR.Activo";
				column = r[index + 7];
				if(column == System.DBNull.Value || column is bool) {
					ssActivo = (column != System.DBNull.Value ?(bool) column: false);
				} else {
					ssActivo = Convert.ToBoolean(column);
				}
			} catch(Exception excep) {
				throw new System.InvalidCastException("Could not assign '" + column + "' to '" + columnName + "'... " + excep.Message);
			}
			index += 8;
		}

		public static bool operator ==(STEvento_RRStructure a, STEvento_RRStructure b) {
			if(a.ssId != b.ssId) return false;
			if(a.ssVelocidade != b.ssVelocidade) return false;
			if(a.ssMatricula != b.ssMatricula) return false;
			if(!RuntimePlatformUtils.CompareByteArrays(a.ssImagem, b.ssImagem)) return false;
			if(a.ssNomeLocal != b.ssNomeLocal) return false;
			if(a.ssUIDLocal != b.ssUIDLocal) return false;
			if(a.ssLimiteVelocidade != b.ssLimiteVelocidade) return false;
			if(a.ssActivo != b.ssActivo) return false;
			return true;
		}

		public static bool operator !=(STEvento_RRStructure a, STEvento_RRStructure b) {
			return !(a==b);
		}

		public override bool Equals(object o) {
			if(o.GetType() != typeof(STEvento_RRStructure)) return false;
			return(this ==(STEvento_RRStructure) o);
		}

		public override int GetHashCode() {
			try {
				return base.GetHashCode()
				^ ssId.GetHashCode()
				^ ssVelocidade.GetHashCode()
				^ ssMatricula.GetHashCode()
				^ ssImagem.GetHashCode()
				^ ssNomeLocal.GetHashCode()
				^ ssUIDLocal.GetHashCode()
				^ ssLimiteVelocidade.GetHashCode()
				^ ssActivo.GetHashCode()
				;
			} catch {
				return base.GetHashCode();
			}
		}

		public void GetObjectData(SerializationInfo info, StreamingContext context) {
			Type objInfo = this.GetType();
			FieldInfo[] fields;
			fields = objInfo.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
			for(int i = 0; i < fields.Length; i++)
			if(fields[i] .FieldType.IsSerializable)
			info.AddValue(fields[i] .Name, fields[i] .GetValue(this));
		}

		public STEvento_RRStructure(SerializationInfo info, StreamingContext context) {
			OptimizedAttributes = null;
			ssId = 0;
			ssVelocidade = 0.0M;
			ssMatricula = string.Empty;
			ssImagem = new byte[] {};
			ssNomeLocal = string.Empty;
			ssUIDLocal = string.Empty;
			ssLimiteVelocidade = 0.0M;
			ssActivo = false;
			Type objInfo = this.GetType();
			FieldInfo[] fields;
			fields = objInfo.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
			if(fields[0] .FieldType.IsSerializable)
			ssId = (int) info.GetValue(fields[0] .Name, fields[0] .FieldType);
			if(fields[1] .FieldType.IsSerializable)
			ssVelocidade = (decimal) info.GetValue(fields[1] .Name, fields[1] .FieldType);
			if(fields[2] .FieldType.IsSerializable)
			ssMatricula = (string) info.GetValue(fields[2] .Name, fields[2] .FieldType);
			if(fields[3] .FieldType.IsSerializable)
			ssImagem = (byte[]) info.GetValue(fields[3] .Name, fields[3] .FieldType);
			if(fields[4] .FieldType.IsSerializable)
			ssNomeLocal = (string) info.GetValue(fields[4] .Name, fields[4] .FieldType);
			if(fields[5] .FieldType.IsSerializable)
			ssUIDLocal = (string) info.GetValue(fields[5] .Name, fields[5] .FieldType);
			if(fields[6] .FieldType.IsSerializable)
			ssLimiteVelocidade = (decimal) info.GetValue(fields[6] .Name, fields[6] .FieldType);
			if(fields[7] .FieldType.IsSerializable)
			ssActivo = (bool) info.GetValue(fields[7] .Name, fields[7] .FieldType);
		}

		public void RecursiveReset() {
		}

		public void InternalRecursiveSave() {
		}


		public STEvento_RRStructure Duplicate() {
			STEvento_RRStructure t;
			t.ssId = this.ssId;
			t.ssVelocidade = this.ssVelocidade;
			t.ssMatricula = this.ssMatricula;
			if(this.ssImagem != null) {
				t.ssImagem = (byte[]) this.ssImagem.Clone();
			} else {
				t.ssImagem = null;
			}
			t.ssNomeLocal = this.ssNomeLocal;
			t.ssUIDLocal = this.ssUIDLocal;
			t.ssLimiteVelocidade = this.ssLimiteVelocidade;
			t.ssActivo = this.ssActivo;
			t.OptimizedAttributes = null;
			return t;
		}

		public void ToXml(Object parent, System.Xml.XmlElement baseElem, String fieldName, int detailLevel) {
			System.Xml.XmlElement entElem = VarValue.AppendChild(baseElem, "Structure");
			if(fieldName != null) {
				VarValue.AppendAttribute(entElem, "debug.field", fieldName);
				fieldName = fieldName.ToLowerInvariant();
			}
			if(detailLevel > 0) {
				if(!VarValue.FieldIsOptimized(parent, fieldName + ".Id")) VarValue.AppendAttribute(entElem, "Id", ssId, detailLevel, TypeKind.Integer); else VarValue.AppendOptimizedAttribute(entElem, "Id");
				if(!VarValue.FieldIsOptimized(parent, fieldName + ".Velocidade")) VarValue.AppendAttribute(entElem, "Velocidade", ssVelocidade, detailLevel, TypeKind.Decimal); else VarValue.AppendOptimizedAttribute(entElem, "Velocidade");
				if(!VarValue.FieldIsOptimized(parent, fieldName + ".Matricula")) VarValue.AppendAttribute(entElem, "Matricula", ssMatricula, detailLevel, TypeKind.Text); else VarValue.AppendOptimizedAttribute(entElem, "Matricula");
				if(!VarValue.FieldIsOptimized(parent, fieldName + ".Imagem")) VarValue.AppendAttribute(entElem, "Imagem", ssImagem, detailLevel, TypeKind.BinaryData); else VarValue.AppendOptimizedAttribute(entElem, "Imagem");
				if(!VarValue.FieldIsOptimized(parent, fieldName + ".NomeLocal")) VarValue.AppendAttribute(entElem, "NomeLocal", ssNomeLocal, detailLevel, TypeKind.Text); else VarValue.AppendOptimizedAttribute(entElem, "NomeLocal");
				if(!VarValue.FieldIsOptimized(parent, fieldName + ".UIDLocal")) VarValue.AppendAttribute(entElem, "UIDLocal", ssUIDLocal, detailLevel, TypeKind.Text); else VarValue.AppendOptimizedAttribute(entElem, "UIDLocal");
				if(!VarValue.FieldIsOptimized(parent, fieldName + ".LimiteVelocidade")) VarValue.AppendAttribute(entElem, "LimiteVelocidade", ssLimiteVelocidade, detailLevel, TypeKind.Decimal); else VarValue.AppendOptimizedAttribute(entElem, "LimiteVelocidade");
				if(!VarValue.FieldIsOptimized(parent, fieldName + ".Activo")) VarValue.AppendAttribute(entElem, "Activo", ssActivo, detailLevel, TypeKind.Boolean); else VarValue.AppendOptimizedAttribute(entElem, "Activo");
			} else {
				VarValue.AppendDeferredEvaluationElement(entElem);
			}
		}

		public void EvaluateFields(VarValue variable, Object parent, String baseName, String fields) {
			String head = VarValue.GetHead(fields);
			String tail = VarValue.GetTail(fields);
			variable.Found = false;
			if(head == "id") {
				if(!VarValue.FieldIsOptimized(parent, baseName + ".Id")) variable.Value = ssId; else variable.Optimized = true;
			} else if(head == "velocidade") {
				if(!VarValue.FieldIsOptimized(parent, baseName + ".Velocidade")) variable.Value = ssVelocidade; else variable.Optimized = true;
			} else if(head == "matricula") {
				if(!VarValue.FieldIsOptimized(parent, baseName + ".Matricula")) variable.Value = ssMatricula; else variable.Optimized = true;
			} else if(head == "imagem") {
				if(!VarValue.FieldIsOptimized(parent, baseName + ".Imagem")) variable.Value = ssImagem; else variable.Optimized = true;
			} else if(head == "nomelocal") {
				if(!VarValue.FieldIsOptimized(parent, baseName + ".NomeLocal")) variable.Value = ssNomeLocal; else variable.Optimized = true;
			} else if(head == "uidlocal") {
				if(!VarValue.FieldIsOptimized(parent, baseName + ".UIDLocal")) variable.Value = ssUIDLocal; else variable.Optimized = true;
			} else if(head == "limitevelocidade") {
				if(!VarValue.FieldIsOptimized(parent, baseName + ".LimiteVelocidade")) variable.Value = ssLimiteVelocidade; else variable.Optimized = true;
			} else if(head == "activo") {
				if(!VarValue.FieldIsOptimized(parent, baseName + ".Activo")) variable.Value = ssActivo; else variable.Optimized = true;
			}
			if(variable.Found && tail != null) variable.EvaluateFields(this, head, tail);
		}

		public bool ChangedAttributeGet(GlobalObjectKey key) {
			throw new Exception("Method not Supported");
		}

		public bool OptimizedAttributeGet(GlobalObjectKey key) {
			throw new Exception("Method not Supported");
		}

		public object AttributeGet(GlobalObjectKey key) {
			if(key == OriginalKeyId) {
				return ssId;
			} else if(key == OriginalKeyVelocidade) {
				return ssVelocidade;
			} else if(key == OriginalKeyMatricula) {
				return ssMatricula;
			} else if(key == OriginalKeyImagem) {
				return ssImagem;
			} else if(key == OriginalKeyNomeLocal) {
				return ssNomeLocal;
			} else if(key == OriginalKeyUIDLocal) {
				return ssUIDLocal;
			} else if(key == OriginalKeyLimiteVelocidade) {
				return ssLimiteVelocidade;
			} else if(key == OriginalKeyActivo) {
				return ssActivo;
			} else {
				throw new Exception("Invalid key");
			}
		}
		public void FillFromOther(ISimpleRecord other) {
			if(other == null) return;
			ssId = (int) other.AttributeGet(OriginalKeyId);
			ssVelocidade = (decimal) other.AttributeGet(OriginalKeyVelocidade);
			ssMatricula = (string) other.AttributeGet(OriginalKeyMatricula);
			ssImagem = (byte[]) other.AttributeGet(OriginalKeyImagem);
			ssNomeLocal = (string) other.AttributeGet(OriginalKeyNomeLocal);
			ssUIDLocal = (string) other.AttributeGet(OriginalKeyUIDLocal);
			ssLimiteVelocidade = (decimal) other.AttributeGet(OriginalKeyLimiteVelocidade);
			ssActivo = (bool) other.AttributeGet(OriginalKeyActivo);
		}
	} // STEvento_RRStructure

} // OutSystems.NssRemotingRadarsAdapter
